package com.peaker.advertisingpro.activity

import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import com.google.gson.Gson
import com.peaker.advertisingpro.R
import com.peaker.advertisingpro.base.BaseActivity
import com.peaker.advertisingpro.bean.*
import com.peaker.advertisingpro.tcp.RequestCallback
import com.peaker.advertisingpro.tcp.ResponseCallback
import com.peaker.advertisingpro.tcp.TCPClient
import com.peaker.advertisingpro.utils.*
import com.peaker.advertisingpro.view.LoginDialog
import com.peaker.advertisingpro.view.PortSetDialog
import kotlinx.android.synthetic.main.activity_login.*


class LoginActivity : BaseActivity(), View.OnClickListener {
    private var heartJson = Gson().toJson(HeartBean())

    companion object instance {
        var loginAc: Activity? = null
        var isClickLogin = false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        loginAc = this
        btn_back.setOnClickListener(this)
        btn_cancel.setOnClickListener(this)
//        et_ip_set
        btn_login.setOnClickListener(this)
        btn_app_set.setOnClickListener(this)
        btn_serve_port.setOnClickListener(this)
        btn_cancel.setOnClickListener(this)
        et_ip_set.setText(
            if (TextUtils.isEmpty(SPUtil.getSP(Common.IP))) Common.ip else SPUtil.getSP(
                Common.IP
            )
        )
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.btn_back -> {
                finish()
            }
            R.id.btn_cancel -> {
                finish()
            }
            R.id.btn_app_set -> {

                //自动检测
                var ipString = et_ip_set.text.toString()
                SPUtil.putSP(Common.IP, ipString)
                tcpConnect(
                    MsgUtils.stringToByte(
                        heartJson
                    )
                )
            }
            R.id.btn_login -> {
                isClickLogin = true
                //登录
                var ip = et_ip_set.text.toString()
                SPUtil.putSP(Common.IP, ip)
                var registData = registData()
                registData.deviceId = SmdtManagerUtils.getAndroid_ID()
                registData.diskSpace =
                    "${SmdtManagerUtils.getAvailableMemory()}/ ${SmdtManagerUtils.getInternalStorageMemory()}"
                registData.firmware = SmdtManagerUtils.getFormattedKernelVersion()
                registData.hardVersion = SmdtManagerUtils.getAndroidModel()
                registData.systemType = SmdtManagerUtils.getDeviceBrand()
                registData.softVersion = packageManager.getPackageInfo(packageName, 0).versionName
                registData.module = "1"
                registData.type = "1"
                registData.screen = SPUtil.getSP(Common.ANGLE, "0")!!
                registData.resolution =
                    "${SmdtManagerUtils.smdtGetScreenWidth()} # ${SmdtManagerUtils.smdtGetScreenHeight()}"
                registData.mac = SmdtManagerUtils.smdtGetEthMacAddress()
                registData.ip = SmdtManagerUtils.smdtGetEthIPAddress()
                registData.volume = SmdtManagerUtils.getCurrentSysVolume().toString()
                registData.isMute = if (SmdtManagerUtils.getCurrentSysVolume() == 0) "1" else "0"
                registData.isActive = if (SPUtil.getSP(Common.AUTH, "无") == "无") "0" else "1"
                registData.isBlacklight =
                    if (SmdtManagerUtils.smdtGetLcdLightStatus() == 0) "1" else "0"
                registData.isRootStart = if (SPUtil.getSP(Common.REBOOT) == "开") "1" else "0"
                registData.isGuard = if (SPUtil.getSP(Common.PROTECT) == "开") "1" else "0"
                registData.isStatus = if (SmdtManagerUtils.smdtGetStatusBar() == 1) "1" else "0"
                registData.screenAngle = SPUtil.getSP(Common.ANGLE, "0") + ""
                var registBean = RegistBean(registData, "2")

                var gson = Gson()

                var loginJson = gson.toJson(ResultJson(gson.toJson(loginBean())))
                SPUtil.putSP(Common.LOGINJSON, loginJson)
                (ScrollActivity.scrollAsc as ScrollActivity).tcpConnect(
                    MsgUtils.stringToByte(
                        loginJson
                    )
                )


            }
            R.id.btn_serve_port -> {
                //  设置端口
                PortSetDialog(this).show()

            }

        }
    }

    override fun onDestroy() {
        isClickLogin = false
        super.onDestroy()
    }

    /**
     * tcp连接
     */
    fun tcpConnect(byteArray: ByteArray) {
        var ip = SPUtil.getSP(Common.IP, Common.ip)
        if (ip.isNullOrEmpty()) {
            ToastUtils.s("ip为空")
            return
        }
        var loginDialog = LoginDialog(this)
        loginDialog.show()
        var iport = SPUtil.getSP(Common.MSG_PORT, Common.msg_port)
        TCPClient.build()
            .server(ip, iport!!.toInt())
            .connTimeout(10 * 1000)
            .request("".toByteArray(), 10000, object : RequestCallback() {
                override fun onConnect() {

                    ToastUtils.s("连接成功")
                    if (loginDialog.isShowing) {
                        loginDialog.dismiss()
                    }
                }

                override fun onFail(throwable: Throwable?) {
                    ToastUtils.s("连接失败")
                    SPUtil.putSP(Common.IP, "")
                    if (loginDialog.isShowing) {
                        loginDialog.dismiss()
                    }
                }

                override fun onTimeout() {
                    ToastUtils.s("连接超时")
                    SPUtil.putSP(Common.IP, "")
                    if (loginDialog.isShowing) {
                        loginDialog.dismiss()
                    }
                }
            }, object : ResponseCallback() {
                override fun onRec(str: String) {

                }

                override fun onFail(throwable: Throwable?) {

                }
            })
    }

}